﻿#include "tcpclient.h"
#include "mainwindow.h"
#include <QDate>
#include <iostream>
tcpclient::tcpclient(QObject *parent) : QObject(parent)
{
    qRegisterMetaType<Qt::GlobalColor>("Qt::GlobalColor"); // 注册Qt::GlobalColor类型
}

tcpclient::~tcpclient()
{
    delete tcpSocket;
}

void tcpclient::creattcpclient()
{
    tcpSocket = new QTcpSocket;
    connect(tcpSocket, &QTcpSocket::connected,
            [=]()
            {
                emit connect_done();
                emit recivedone("服务器连接", Qt::GlobalColor::darkGreen);
                conectflag = true;
            });
    connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnectnet()));
    connect(tcpSocket, &QTcpSocket::readyRead, this, &tcpclient::read);
}

void tcpclient::write(const char *data, qint64 len)
{
    tcpSocket->write(data, len);
}

#if 0
void tcpclient::read()
{
    if(tcpSocket->bytesAvailable() <= 0) { //tcpSocket缓冲区没有数据直接返回
        return;
    }
    QByteArray buffer = tcpSocket->readAll();
    m_buffer.append(buffer);        //将读取的数据放入包缓冲区
    unsigned char KeyWord;
    int datalen;
    //int total_length = m_buffer.size();
    //while(total_length)
    //{
    QDataStream recv_data(buffer);
    recv_data.setVersion(QDataStream::Qt_5_13);
    recv_data >> KeyWord;
    // }
    qDebug() << "KeyWord:" << KeyWord;
}

#else

void tcpclient::read()
{
    pack_Control_Head *get_Data;
    m_pack_Control_Head = new pack_Control_Head;
    QByteArray buffer;
    // 读取缓冲区数据
    buffer.append(tcpSocket->readAll());
    int length = buffer.length();
    if (length >= 18)
    {
        memset(m_pack_Control_Head, 0, sizeof(pack_Control_Head));
        get_Data = (pack_Control_Head *)buffer.data();
        for (int i = 0; i < sizeof(m_pack_Control_Head->Massage_Header); i++)
        {
            m_pack_Control_Head->Massage_Header = get_Data->Massage_Header;
        }
        m_pack_Control_Head->KeyWord = get_Data->KeyWord;
        m_pack_Control_Head->Opreation = get_Data->Opreation;
        if (m_pack_Control_Head->Massage_Header == 0xa55ab55b && m_pack_Control_Head->Opreation == 0x81)
        {
            switch (m_pack_Control_Head->KeyWord)
            {
            case 0x0101:
            {
                pack_Control_To_PC_SystemTime *systime_char;
                memset(&m_pack_Control_To_PC_SystemTime, 0, sizeof(pack_Control_To_PC_SystemTime));
                systime_char = (pack_Control_To_PC_SystemTime *)buffer.data();
                memcpy(&m_pack_Control_To_PC_SystemTime, systime_char, sizeof(pack_Control_To_PC_SystemTime));
                if (m_pack_Control_To_PC_SystemTime.Massage_Tail == 0xff0d)
                {
                    fSystemTime_flag = 1;
                    QDateTime dd = QDateTime::fromMSecsSinceEpoch(m_pack_Control_To_PC_SystemTime.systime);
                    qDebug() << dd.toString("yyyy-MM-dd hh:mm:ss");
                }
                break;
            }
            case 0x0102:
            {

                if (m_pack_Control_Head->Device_Address == 0x01)
                {
                    pack_Control_To_SignalType_H *signaltype_char;
                    memset(&m_pack_Control_To_SignalType_H, 0, sizeof(m_pack_Control_To_SignalType_H));
                    signaltype_char = (pack_Control_To_SignalType_H *)buffer.data();
                    memcpy(&m_pack_Control_To_SignalType_H, signaltype_char, sizeof(m_pack_Control_To_SignalType_H));
                    if (m_pack_Control_To_SignalType_H.Massage_Tail == 0xff0d)
                    {
                        fSignalType_flag_H = 1;
                        qDebug() << "%d" << m_pack_Control_To_SignalType_H.signaltype_H;
                        //                qDebug() << dd.toString("yyyy-MM-dd hh:mm:ss");
                    }
                }
                else if (m_pack_Control_Head->Device_Address == 0x02)
                {
                    pack_Control_To_SignalType_L *signaltype_char;
                    memset(&m_pack_Control_To_SignalType_L, 0, sizeof(m_pack_Control_To_SignalType_L));
                    signaltype_char = (pack_Control_To_SignalType_L *)buffer.data();
                    memcpy(&m_pack_Control_To_SignalType_L, signaltype_char, sizeof(m_pack_Control_To_SignalType_L));
                    if (m_pack_Control_To_SignalType_L.Massage_Tail == 0xff0d)
                    {
                        fSignalType_flag_L = 1;
                        qDebug() << "%d" << m_pack_Control_To_SignalType_L.signaltype_L;
                        //                qDebug() << dd.toString("yyyy-MM-dd hh:mm:ss");
                    }
                }

                break;
            }
            case 0x0104:
            {
                pack_PC_To_Control_TVG_Gain *Gain_char;
                memset(&m_pack_PC_To_Control_TVG_Gain, 0, sizeof(pack_PC_To_Control_TVG_Gain));
                Gain_char = (pack_PC_To_Control_TVG_Gain *)buffer.data();
                memcpy(&m_pack_PC_To_Control_TVG_Gain, Gain_char, sizeof(pack_PC_To_Control_TVG_Gain));
                if (m_pack_PC_To_Control_TVG_Gain.Massage_Tail == 0xff0d)
                {
                    fGain_flag = 1;
                    qDebug() << "%d" << m_pack_PC_To_Control_TVG_Gain.gain;
                }
                break;
            }
            case 0x0105:
            {
                pack_PC_To_Control_WorkPower *WorkPower_char;
                memset(&m_pack_PC_To_Control_WorkPower, 0, sizeof(pack_PC_To_Control_WorkPower));
                WorkPower_char = (pack_PC_To_Control_WorkPower *)buffer.data();
                memcpy(&m_pack_PC_To_Control_WorkPower, WorkPower_char, sizeof(pack_PC_To_Control_WorkPower));
                if (m_pack_PC_To_Control_WorkPower.Massage_Tail == 0xff0d)
                {
                    fWorkPower_flag = 1;
                    qDebug() << "work_power:%d" << m_pack_PC_To_Control_WorkPower.work_power;
                }
                break;
            }
            case 0x0106:
            {
                if (m_pack_Control_Head->Device_Address == 0x01)
                {
                    pack_PC_To_Control_TVG_H *TVG_char_H;
                    memset(&m_pack_PC_To_Control_TVG_H, 0, sizeof(pack_PC_To_Control_TVG_H));
                    TVG_char_H = (pack_PC_To_Control_TVG_H *)buffer.data();
                    memcpy(&m_pack_PC_To_Control_TVG_H, TVG_char_H, sizeof(pack_PC_To_Control_TVG_H));
                    if (m_pack_PC_To_Control_TVG_H.Massage_Tail == 0xff0d)
                    {
                        fTVG_flag_H = 1;
                        qDebug() << "A:%f" << m_pack_PC_To_Control_TVG_H.m_TVG_H.TVG_Alpha;
                        qDebug() << "B:%f" << m_pack_PC_To_Control_TVG_H.m_TVG_H.TVG_Beta;
                        qDebug() << "Y:%f" << m_pack_PC_To_Control_TVG_H.m_TVG_H.TVG_Gamma;
                    }
                }
                else if (m_pack_Control_Head->Device_Address == 0x02)
                {
                    pack_PC_To_Control_TVG_L *TVG_char_L;
                    memset(&m_pack_PC_To_Control_TVG_L, 0, sizeof(pack_PC_To_Control_TVG_L));
                    TVG_char_L = (pack_PC_To_Control_TVG_L *)buffer.data();
                    memcpy(&m_pack_PC_To_Control_TVG_L, TVG_char_L, sizeof(pack_PC_To_Control_TVG_L));
                    if (m_pack_PC_To_Control_TVG_L.Massage_Tail == 0xff0d)
                    {
                        fTVG_flag_L = 1;
                        qDebug() << "A:%f" << m_pack_PC_To_Control_TVG_L.m_TVG_L.TVG_Alpha;
                        qDebug() << "B:%f" << m_pack_PC_To_Control_TVG_L.m_TVG_L.TVG_Beta;
                        qDebug() << "Y:%f" << m_pack_PC_To_Control_TVG_L.m_TVG_L.TVG_Gamma;
                    }
                }

                break;
            }
            case 0x0107:
            {
                pack_PC_To_Control_WorkMode *WorkMode_char;
                memset(&m_pack_PC_To_Control_WorkMode, 0, sizeof(pack_PC_To_Control_WorkMode));
                WorkMode_char = (pack_PC_To_Control_WorkMode *)buffer.data();
                memcpy(&m_pack_PC_To_Control_WorkMode, WorkMode_char, sizeof(pack_PC_To_Control_WorkMode));
                if (m_pack_PC_To_Control_WorkMode.Massage_Tail == 0xff0d)
                {
                    fWorkMode_flag = 1;
                    qDebug() << "workmode:%d" << m_pack_PC_To_Control_WorkMode.workmode;
                }
                break;
            }
            case 0x0150:
            {
                pack_Control_To_PC_TEMP *TEMP_char;
                memset(&m_pack_Control_To_PC_TEMP, 0, sizeof(pack_Control_To_PC_TEMP));
                TEMP_char = (pack_Control_To_PC_TEMP *)buffer.data();
                memcpy(&m_pack_Control_To_PC_TEMP, TEMP_char, sizeof(pack_Control_To_PC_TEMP));
                if (m_pack_Control_To_PC_TEMP.Massage_Tail == 0xff0d)
                {
                    fTEMP_flag = 1;
                    qDebug() << "TEMP[0]:%f" << m_pack_Control_To_PC_TEMP.m_Temp_Data.Temperature_one;
                    qDebug() << "TEMP[1]:%f" << m_pack_Control_To_PC_TEMP.m_Temp_Data.Temperature_second;
                    qDebug() << "TEMP[2]:%f" << m_pack_Control_To_PC_TEMP.m_Temp_Data.Temperature_Three;
                }
                break;
            }
            case 0x0151:
            {
                pack_Control_To_PC_Humidity *Humidity_char;
                memset(&m_pack_Control_To_PC_Humidity, 0, sizeof(pack_Control_To_PC_Humidity));
                Humidity_char = (pack_Control_To_PC_Humidity *)buffer.data();
                memcpy(&m_pack_Control_To_PC_Humidity, Humidity_char, sizeof(pack_Control_To_PC_Humidity));
                if (m_pack_Control_To_PC_Humidity.Massage_Tail == 0xff0d)
                {
                    fHumidity_flag = 1;
                    qDebug() << "Humidity[0]:%f" << m_pack_Control_To_PC_Humidity.m_Humidity_Data.Hmidity_one;
                    qDebug() << "Humidity[1]:%f" << m_pack_Control_To_PC_Humidity.m_Humidity_Data.Hmidity_second;
                    qDebug() << "Humidity[2]:%f" << m_pack_Control_To_PC_Humidity.m_Humidity_Data.Hmidity_Three;
                }
                break;
            }
            case 0x0152:
            {
                pack_Control_To_PC_SASTankStatus *SASTankStatus_char;
                memset(&m_pack_Control_To_PC_SASTankStatus, 0, sizeof(pack_Control_To_PC_SASTankStatus));
                SASTankStatus_char = (pack_Control_To_PC_SASTankStatus *)buffer.data();
                memcpy(&m_pack_Control_To_PC_SASTankStatus, SASTankStatus_char, sizeof(pack_Control_To_PC_SASTankStatus));
                if (m_pack_Control_To_PC_SASTankStatus.Massage_Tail == 0xff0d)
                {
                    fSASTankStatus_flag = 1;
                    qDebug() << "fTotalStrage:%f" << m_pack_Control_To_PC_SASTankStatus.m_SASTankStatus.fTotalStrage;
                    qDebug() << "fUseStarge:%f" << m_pack_Control_To_PC_SASTankStatus.m_SASTankStatus.fUseStarge;
                    qDebug() << "fPercentage:%f" << m_pack_Control_To_PC_SASTankStatus.m_SASTankStatus.fPercentage;
                }
                break;
            }
            case 0x0153:
            {
                pack_Control_To_PC_PressureHeight *PressureHeight_char;
                memset(&m_pack_Control_To_PC_PressureHeight, 0, sizeof(pack_Control_To_PC_PressureHeight));
                PressureHeight_char = (pack_Control_To_PC_PressureHeight *)buffer.data();
                memcpy(&m_pack_Control_To_PC_PressureHeight, PressureHeight_char, sizeof(pack_Control_To_PC_PressureHeight));
                if (m_pack_Control_To_PC_PressureHeight.Massage_Tail == 0xff0d)
                {
                    fPressureHeight_flag = 1;
                    qDebug() << "Sensor_id:%d" << m_pack_Control_To_PC_PressureHeight.m_PressureHeight.Sensor_id;
                    qDebug() << "fHeight:%f" << m_pack_Control_To_PC_PressureHeight.m_PressureHeight.fHeight;
                }
                break;
            }
            case 0x0154:
            {
                pack_PC_To_Control_PressureHeight_EN *PressureHeightEN_char;
                memset(&m_pack_PC_To_Control_PressureHeight_EN, 0, sizeof(pack_PC_To_Control_PressureHeight_EN));
                PressureHeightEN_char = (pack_PC_To_Control_PressureHeight_EN *)buffer.data();
                memcpy(&m_pack_PC_To_Control_PressureHeight_EN, PressureHeightEN_char, sizeof(pack_PC_To_Control_PressureHeight_EN));
                if (m_pack_PC_To_Control_PressureHeight_EN.Massage_Tail == 0xff0d)
                {
                    fPressureHeightEN_flag = 1;
                    qDebug() << "PressureHeight_EN:%d" << m_pack_PC_To_Control_PressureHeight_EN.PressureHeight_EN;
                }
                break;
            }
            case 0x0155:
            {
                pack_Control_To_PC_AltitudeSensor *AltitudeSensor_char;
                memset(&m_pack_Control_To_PC_AltitudeSensor, 0, sizeof(pack_Control_To_PC_AltitudeSensor));
                AltitudeSensor_char = (pack_Control_To_PC_AltitudeSensor *)buffer.data();
                memcpy(&m_pack_Control_To_PC_AltitudeSensor, AltitudeSensor_char, sizeof(pack_Control_To_PC_AltitudeSensor));
                if (m_pack_Control_To_PC_AltitudeSensor.Massage_Tail == 0xff0d)
                {
                    fAltitudeSensor_flag = 1;
                    qDebug() << "Altitude_id:%d" << m_pack_Control_To_PC_AltitudeSensor.m_AltitudeSensor.Altitude_id;
                    qDebug() << "fHeading:%f" << m_pack_Control_To_PC_AltitudeSensor.m_AltitudeSensor.fHeading;
                    qDebug() << "fPitch:%f" << m_pack_Control_To_PC_AltitudeSensor.m_AltitudeSensor.fPitch;
                    qDebug() << "fRoll:%f" << m_pack_Control_To_PC_AltitudeSensor.m_AltitudeSensor.fRoll;
                }
                break;
            }
            case 0x0156:
            {
                pack_PC_To_Control_AltitudeSensor_EN *AltitudeSensorEN_char;
                memset(&m_pack_PC_To_Control_AltitudeSensor_EN, 0, sizeof(pack_PC_To_Control_AltitudeSensor_EN));
                AltitudeSensorEN_char = (pack_PC_To_Control_AltitudeSensor_EN *)buffer.data();
                memcpy(&m_pack_PC_To_Control_AltitudeSensor_EN, AltitudeSensorEN_char, sizeof(pack_PC_To_Control_AltitudeSensor_EN));
                if (m_pack_PC_To_Control_AltitudeSensor_EN.Massage_Tail == 0xff0d)
                {
                    fAltitudeSensorEN_flag = 1;
                    qDebug() << "AltitudeSensor_EN:%d" << m_pack_PC_To_Control_AltitudeSensor_EN.AltitudeSensor_EN;
                }
                break;
            }
            case 0x0157:
            {
                pack_PC_To_Control_SoundVelocity *SoundVelocity_char;
                memset(&m_pack_PC_To_Control_SoundVelocity, 0, sizeof(pack_PC_To_Control_SoundVelocity));
                SoundVelocity_char = (pack_PC_To_Control_SoundVelocity *)buffer.data();
                memcpy(&m_pack_PC_To_Control_SoundVelocity, SoundVelocity_char, sizeof(pack_PC_To_Control_SoundVelocity));
                if (m_pack_PC_To_Control_SoundVelocity.Massage_Tail == 0xff0d)
                {
                    fSoundVelocity_flag = 1;
                    qDebug() << "sound_velocity:%d" << m_pack_PC_To_Control_SoundVelocity.sound_velocity;
                }
                break;
            }
            case 0x0181:
            {
                pack_Control_To_PC_Altimeter *Altimeter_char;
                memset(&m_pack_Control_To_PC_Altimeter, 0, sizeof(pack_Control_To_PC_Altimeter));
                Altimeter_char = (pack_Control_To_PC_Altimeter *)buffer.data();
                memcpy(&m_pack_Control_To_PC_Altimeter, Altimeter_char, sizeof(pack_Control_To_PC_Altimeter));
                if (m_pack_Control_To_PC_Altimeter.Massage_Tail == 0xff0d)
                {
                    fAltimeter_flag = 1;
                    for (int j = 0; j < 6; j++)
                    {
                        emit getheigth(m_pack_Control_To_PC_Altimeter.data[j], Qt::GlobalColor::darkGreen);
                    }
                }
                break;
            }
            case 0x0901:
            {
                Device_Information *Infromtion_char;
                QString add;
                memset(&m_Device_Information, 0, sizeof(Device_Information));
                Infromtion_char = (Device_Information *)buffer.data();
                memcpy(&m_Device_Information, Infromtion_char, sizeof(Device_Information));
                if (m_Device_Information.Massage_Tail == 0xff0d)
                {
                    finformation_flag = 1;
                    qDebug() << "sonar:" << m_Device_Information.m_information.sonar;
                    QString add = "sonar:";
                    add.append(QString::number(m_Device_Information.m_information.sonar, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                    // MainWindow:addLogMess(add,Qt::GlobalColor::darkBlue);
                    qDebug() << "sorting:" << m_Device_Information.m_information.sorting;
                    add = "sorting:";
                    add.append(QString::number(m_Device_Information.m_information.sorting, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                    qDebug() << "years:" << m_Device_Information.m_information.years;
                    add = "years:";
                    add.append(QString::number(m_Device_Information.m_information.years, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                    qDebug() << "numbering:" << m_Device_Information.m_information.numbering;
                    add = "numbering:";
                    add.append(QString::number(m_Device_Information.m_information.numbering, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                    qDebug() << "firm_year:" << m_Device_Information.m_information.firm_year;
                    add = "firm_year:";
                    add.append(QString::number(m_Device_Information.m_information.firm_year, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                    qDebug() << "firm_month:" << m_Device_Information.m_information.firm_month;
                    add = "firm_month:";
                    add.append(QString::number(m_Device_Information.m_information.firm_month, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                    qDebug() << "version_number:" << m_Device_Information.m_information.version_number;
                    add = "version_number:";
                    add.append(QString::number(m_Device_Information.m_information.version_number, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                    qDebug() << "license_time:" << m_Device_Information.m_information.license_time;
                    add = "license_time:";
                    add.append(QString::number(m_Device_Information.m_information.license_time, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                }

                break;
            }
            case 0x015C:
            {
                pack_PC_To_Control_Voltage_And_Current *Voltage_And_Current_char;
                QString add;
                memset(&m_Voltage_And_Current, 0, sizeof(pack_PC_To_Control_Voltage_And_Current));
                Voltage_And_Current_char = (pack_PC_To_Control_Voltage_And_Current *)buffer.data();
                memcpy(&m_Voltage_And_Current, Voltage_And_Current_char, sizeof(pack_PC_To_Control_Voltage_And_Current));
                if (m_Voltage_And_Current.Massage_Tail == 0xff0d)
                {
                    finVA_flag = 1;
                    qDebug() << "Count:" << m_Voltage_And_Current.m_Voltage_And_Current_Data.Count;
                    QString add = "Count:";
                    add.append(QString::number(m_Voltage_And_Current.m_Voltage_And_Current_Data.Count, 16));
                    qDebug() << "Main_V:" << m_Voltage_And_Current.m_Voltage_And_Current_Data.Main_V;
                    add = "Main_V:";
                    add.append(QString::number(m_Voltage_And_Current.m_Voltage_And_Current_Data.Main_V, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                    // MainWindow:addLogMess(add,Qt::GlobalColor::darkBlue);
                    qDebug() << "Main_A:" << m_Voltage_And_Current.m_Voltage_And_Current_Data.Main_A;
                    add = "Main_A:";
                    add.append(QString::number(m_Voltage_And_Current.m_Voltage_And_Current_Data.Main_A, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                    qDebug() << "CollectA_V:" << m_Voltage_And_Current.m_Voltage_And_Current_Data.CollectA_V;
                    add = "CollectA_V:";
                    add.append(QString::number(m_Voltage_And_Current.m_Voltage_And_Current_Data.CollectA_V, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                    qDebug() << "CollectA_A:" << m_Voltage_And_Current.m_Voltage_And_Current_Data.CollectA_A;
                    add = "CollectA_A:";
                    add.append(QString::number(m_Voltage_And_Current.m_Voltage_And_Current_Data.CollectA_A, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                    qDebug() << "CollectB_V:" << m_Voltage_And_Current.m_Voltage_And_Current_Data.CollectB_V;
                    add = "CollectB_V:";
                    add.append(QString::number(m_Voltage_And_Current.m_Voltage_And_Current_Data.CollectB_V, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                    qDebug() << "CollectB_A:" << m_Voltage_And_Current.m_Voltage_And_Current_Data.CollectB_A;
                    add = "CollectB_A:";
                    add.append(QString::number(m_Voltage_And_Current.m_Voltage_And_Current_Data.CollectB_A, 16));
                    emit recivedone(add, Qt::GlobalColor::darkGreen);
                }

                break;
            }
            case 0x0111:
            {
                pack_PC_To_Control_GPS_SCOUNT *m_pack_GPS_SCOUNT;
                memset(&m_pack_PC_To_Control_GPS_SCOUNT, 0, sizeof(pack_PC_To_Control_GPS_SCOUNT));
                m_pack_GPS_SCOUNT = (pack_PC_To_Control_GPS_SCOUNT *)buffer.data();
                memcpy(&m_pack_PC_To_Control_GPS_SCOUNT, m_pack_GPS_SCOUNT, sizeof(pack_PC_To_Control_GPS_SCOUNT));
                if (m_pack_PC_To_Control_GPS_SCOUNT.Massage_Tail == 0xff0d)
                {

                    emit updateAltitudeDisplay(m_pack_PC_To_Control_GPS_SCOUNT.SartsCount);
                }
            }
            case 0x0112:
            {
                pack_PC_To_Control_GPS_UTC *m_pack_GPS_UTC;
                memset(&m_pack_PC_To_Control_GPS_UTC, 0, sizeof(pack_PC_To_Control_GPS_UTC));
                m_pack_GPS_UTC = (pack_PC_To_Control_GPS_UTC *)buffer.data();
                memcpy(&m_pack_PC_To_Control_GPS_UTC, m_pack_GPS_UTC, sizeof(pack_PC_To_Control_GPS_UTC));
                if (m_pack_PC_To_Control_GPS_UTC.Massage_Tail == 0xff0d)
                {
                    emit updateUTCDisplay(m_pack_PC_To_Control_GPS_UTC.GPS_UTC);
                }
            }
            case 0x0113:
            {
                pack_PC_To_Control_HCC_STATE *m_pack_HCC_STATE;
                memset(&m_pack_PC_To_Control_HCC_STATE, 0, sizeof(pack_PC_To_Control_HCC_STATE));
                m_pack_HCC_STATE = (pack_PC_To_Control_HCC_STATE *)buffer.data();
                memcpy(&m_pack_PC_To_Control_HCC_STATE, m_pack_HCC_STATE, sizeof(pack_PC_To_Control_HCC_STATE));
                if (m_pack_PC_To_Control_HCC_STATE.Massage_Tail == 0xff0d)
                {
                    emit updateStateDisplay(m_pack_PC_To_Control_HCC_STATE.HCC_State);
                }
            }
            case 0x0114:
            {
                pack_PC_To_Control_HCC_MODE *m_pack_HCC_MODE_STATE;
                memset(&m_pack_Control_To_PC_HCC_MODE, 0, sizeof(pack_PC_To_Control_HCC_MODE));
                m_pack_HCC_MODE_STATE = (pack_PC_To_Control_HCC_MODE *)buffer.data();
                memcpy(&m_pack_Control_To_PC_HCC_MODE, m_pack_HCC_MODE_STATE, sizeof(pack_PC_To_Control_HCC_MODE));
                if (m_pack_Control_To_PC_HCC_MODE.Massage_Tail == 0xff0d)
                {
                    std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<m_pack_Control_To_PC_HCC_MODE.HCC_Mode << std::endl;
                    emit updateStateDisplay_Mode(m_pack_Control_To_PC_HCC_MODE.HCC_Mode);
                }
            }
            }
        }
    }
}

#endif

void tcpclient::read_data()
{
    emit recivedone(QString("收到字节数：%1").arg(tcpSocket->bytesAvailable()), Qt::GlobalColor::blue);
    QString strDisplay;
    QByteArray Read_Temp = tcpSocket->readAll();
    if (Read_Temp.length() > 1000)
    {
        Read_Temp.clear();
    }
    if (/* DISABLES CODE */ (1))
    {                                         // 十六进制显示
        QString a = Read_Temp.toHex().data(); // 十六进制显示
        a = a.toUpper();                      // 转换为大写
        for (int i = 0; i < a.length(); i += 2)
        {
            QString st = a.mid(i, 2); // 返回一个字符串 从指定位置返回n个字符
            strDisplay += st;
            strDisplay += " "; // 每个字节中间的空格
        }
    }
    else
    { // 字符显示
        strDisplay = QString(Read_Temp);
    }
    emit recivedone(QString("收到数据：%1").arg(strDisplay), Qt::GlobalColor::blue);
}

void tcpclient::sendData(const QString &data)
{
    QByteArray buffer;
    if (HexSendTcpClient)
    {
        buffer = hexStrToByteArray(data);
    }
    else if (AsciiTcpClient)
    {
        buffer = asciiStrToByteArray(data);
    }
    else
    {
        buffer = data.toLatin1();
    }
    tcpSocket->write(buffer);
}

int tcpclient::strHexToDecimal(const QString &strHex)
{
    bool ok;
    return strHex.toInt(&ok, 16);
}

char tcpclient::convertHexChar(char ch)
{
    if ((ch >= '0') && (ch <= '9'))
    {
        return ch - 0x30;
    }
    else if ((ch >= 'A') && (ch <= 'F'))
    {
        return ch - 'A' + 10;
    }
    else if ((ch >= 'a') && (ch <= 'f'))
    {
        return ch - 'a' + 10;
    }
    else
    {
        return (-1);
    }
}

QByteArray tcpclient::asciiStrToByteArray(const QString &str)
{
    QByteArray buffer;
    int len = str.length();
    QString letter;
    QString hex;
    for (int i = 0; i < len; i++)
    {
        letter = str.at(i);
        if (letter == "\\")
        {
            i++;
            letter = str.mid(i, 1);
            if (letter == "x")
            {
                i++;
                hex = str.mid(i, 2);
                buffer.append(strHexToDecimal(hex));
                i++;
                continue;
            }
            else if (letter == "N")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "U")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "L")
                    { // NUL=0x00
                        buffer.append((char)0x00);
                        continue;
                    }
                }
                else if (hex == "A")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "K")
                    { // NAK=0x15
                        buffer.append(0x15);
                        continue;
                    }
                }
            }
            else if (letter == "S")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "O")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "H")
                    { // SOH=0x01
                        buffer.append(0x01);
                        continue;
                    }
                    else
                    { // SO=0x0E
                        buffer.append(0x0E);
                        i--;
                        continue;
                    }
                }
                else if (hex == "T")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "X")
                    { // STX=0x02
                        buffer.append(0x02);
                        continue;
                    }
                }
                else if (hex == "I")
                { // SI=0x0F
                    buffer.append(0x0F);
                    continue;
                }
                else if (hex == "Y")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "N")
                    { // SYN=0x16
                        buffer.append(0x16);
                        continue;
                    }
                }
                else if (hex == "U")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "B")
                    { // SUB=0x1A
                        buffer.append(0x1A);
                        continue;
                    }
                }
            }
            else if (letter == "E")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "T")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "X")
                    { // ETX=0x03
                        buffer.append(0x03);
                        continue;
                    }
                    else if (hex == "B")
                    { // ETB=0x17
                        buffer.append(0x17);
                        continue;
                    }
                }
                else if (hex == "O")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "T")
                    { // EOT=0x04
                        buffer.append(0x04);
                        continue;
                    }
                }
                else if (hex == "N")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "Q")
                    { // ENQ=0x05
                        buffer.append(0x05);
                        continue;
                    }
                }
                else if (hex == "M")
                { // EM=0x19
                    buffer.append(0x19);
                    continue;
                }
                else if (hex == "S")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "C")
                    { // ESC=0x1B
                        buffer.append(0x1B);
                        continue;
                    }
                }
            }
            else if (letter == "A")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "C")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "K")
                    { // ACK=0x06
                        buffer.append(0x06);
                        continue;
                    }
                }
            }
            else if (letter == "B")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "E")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "L")
                    { // BEL=0x07
                        buffer.append(0x07);
                        continue;
                    }
                }
                else if (hex == "S")
                { // BS=0x08
                    buffer.append(0x08);
                    continue;
                }
            }
            else if (letter == "C")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "R")
                { // CR=0x0D
                    buffer.append(0x0D);
                    continue;
                }
                else if (hex == "A")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "N")
                    { // CAN=0x18
                        buffer.append(0x18);
                        continue;
                    }
                }
            }
            else if (letter == "D")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "L")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "E")
                    { // DLE=0x10
                        buffer.append(0x10);
                        continue;
                    }
                }
                else if (hex == "C")
                {
                    i++;
                    hex = str.mid(i, 1);
                    if (hex == "1")
                    { // DC1=0x11
                        buffer.append(0x11);
                        continue;
                    }
                    else if (hex == "2")
                    { // DC2=0x12
                        buffer.append(0x12);
                        continue;
                    }
                    else if (hex == "3")
                    { // DC3=0x13
                        buffer.append(0x13);
                        continue;
                    }
                    else if (hex == "4")
                    { // DC2=0x14
                        buffer.append(0x14);
                        continue;
                    }
                }
            }
            else if (letter == "F")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "F")
                { // FF=0x0C
                    buffer.append(0x0C);
                    continue;
                }
                else if (hex == "S")
                { // FS=0x1C
                    buffer.append(0x1C);
                    continue;
                }
            }
            else if (letter == "H")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "T")
                { // HT=0x09
                    buffer.append(0x09);
                    continue;
                }
            }
            else if (letter == "L")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "F")
                { // LF=0x0A
                    buffer.append(0x0A);
                    continue;
                }
            }
            else if (letter == "G")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "S")
                { // GS=0x1D
                    buffer.append(0x1D);
                    continue;
                }
            }
            else if (letter == "R")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "S")
                { // RS=0x1E
                    buffer.append(0x1E);
                    continue;
                }
            }
            else if (letter == "U")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "S")
                { // US=0x1F
                    buffer.append(0x1F);
                    continue;
                }
            }
            else if (letter == "V")
            {
                i++;
                hex = str.mid(i, 1);
                if (hex == "T")
                { // VT=0x0B
                    buffer.append(0x0B);
                    continue;
                }
            }
            else if (letter == "\\")
            {
                // 如果连着的是多个\\则对应添加\对应的16进制0x5C
                buffer.append(0x5C);
                continue;
            }
            else
            {
                // 将对应的\[前面的\\也要加入
                buffer.append(0x5C);
                buffer.append(letter.toLatin1());
                continue;
            }
        }
        buffer.append(str.mid(i, 1).toLatin1());
    }
    return buffer;
}

QByteArray tcpclient::hexStrToByteArray(const QString &str)
{
    QByteArray senddata;
    int hexdata, lowhexdata;
    int hexdatalen = 0;
    int len = str.length();
    senddata.resize(len / 2);
    char lstr, hstr;
    for (int i = 0; i < len;)
    {
        hstr = str.at(i).toLatin1();
        if (hstr == ' ')
        {
            i++;
            continue;
        }
        i++;
        if (i >= len)
        {
            break;
        }
        lstr = str.at(i).toLatin1();
        hexdata = convertHexChar(hstr);
        lowhexdata = convertHexChar(lstr);
        if ((hexdata == 16) || (lowhexdata == 16))
        {
            break;
        }
        else
        {
            hexdata = hexdata * 16 + lowhexdata;
        }
        i++;
        senddata[hexdatalen] = (char)hexdata;
        hexdatalen++;
    }
    senddata.resize(hexdatalen);
    return senddata;
}

void tcpclient::connectnet(QString IP, quint16 PORT)
{
    tcpSocket->abort();
    tcpSocket->connectToHost(QHostAddress(IP), PORT);
}

void tcpclient::disconnectnet()
{
    tcpSocket->disconnectFromHost();
    emit disconnect_done();
    emit recivedone("服务器断开", Qt::GlobalColor::red);
    conectflag = false;
    fAltimeter_data = 0;
}
